package org.biojava.servlets.dazzle.datasource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Matcher;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.biojava.bio.Annotation;
import org.biojava.bio.BioException;
import org.biojava.bio.program.gff.GFFDocumentHandler;
import org.biojava.bio.program.gff.GFFEntrySet;
import org.biojava.bio.program.gff.GFFParser;
import org.biojava.bio.program.gff.GFFRecord;
import org.biojava.bio.seq.DNATools;
import org.biojava.bio.seq.FeatureHolder;
import org.biojava.bio.seq.Sequence;
import org.biojava.bio.seq.impl.SimpleSequence;
import org.biojava.bio.seq.impl.ViewSequence;
import org.biojava.bio.symbol.DummySymbolList;
//import org.biojava.servlets.dazzle.datasource.GFFAnnotationSource.Handler;
import org.biojava.utils.ChangeVetoException;

//import uk.ac.sanger.FeatureBean;

public class SimpleWritebackSource extends AbstractDataSource implements DazzleDataSource {

//public class SimpleWritebackSource implements WritebackSource{

	/*public void writeback(FeatureBean[] features, int command)
			throws DataSourceException {
		// TODO Auto-generated method stub
		
	}*/


	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public boolean doLink(HttpServletRequest req, HttpServletResponse resp,
			String field, String id) throws ServletException, IOException,
			DataSourceException, NoSuchElementException {
		// TODO Auto-generated method stub
		return false;
	}

	public Set getAllTypes() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getDataSourceType() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getDataSourceVersion() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}

	public int getLandmarkLength(String ref) throws DataSourceException,
			NoSuchElementException {
		// TODO Auto-generated method stub
		return 0;
	}

	public String getLandmarkVersion(String ref) throws DataSourceException,
			NoSuchElementException {
		// TODO Auto-generated method stub
		return null;
	}

	public String getMapMaster() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getStylesheet() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getTypeDescription(String type) {
		// TODO Auto-generated method stub
		return null;
	}

	public String getVersion() {
		// TODO Auto-generated method stub
		return null;
	}

	public Sequence getSequence(String ref)
    throws DataSourceException, NoSuchElementException
{
    //ref = mapName(ref);
    int length = Integer.MAX_VALUE;
    /*if (sequenceLengths.containsKey(ref)) {
        length = ((Integer) sequenceLengths.get(ref)).intValue();
    }*/
    return new SimpleSequence(new DummySymbolList(DNATools.getDNA(), length), ref, ref, Annotation.EMPTY_ANNOTATION);
}
	
	
	public void init(ServletContext ctx) 
    throws DataSourceException
{
    super.init(ctx);
    /*try {
        gffSets = new HashMap();
        allTypes = new HashSet();
        
        BufferedReader br = new BufferedReader(new InputStreamReader(ctx.getResourceAsStream(fileName)));
        GFFEntrySet gffe = new GFFEntrySet();
        final GFFDocumentHandler adder = gffe.getAddHandler();
        
        GFFDocumentHandler handler = new Handler();
        GFFParser gffp = new GFFParser();
        gffp.parse(br, handler, fileName);
    } catch (Exception ex) {
        throw new DataSourceException(ex, "Couldn't load GFF");
    }*/
}
	
	/*public FeatureHolder getFeatures(String ref)
    throws NoSuchElementException, DataSourceException
{
    ref = mapName(ref);
    if (!gffSets.containsKey(ref)) {
        return null;
    }
    FeatureHolder features = (FeatureHolder) featureSets.get(ref);
    if (features == null) {
        try {
            Sequence seq = getSequence(ref);
            ViewSequence vseq = new ViewSequence(seq);
            annotate(vseq);
            
            features = vseq.getAddedFeatures();
            featureSets.put(ref, features);
        } catch (BioException ex) {
            throw new DataSourceException(ex, "Error annotating sequence " + ref);
        } catch (ChangeVetoException ex) {
            throw new DataSourceException("ViewSequence isn't accepting features :(");
        }	    
    } 
    return features;
}*/


    private class Handler implements GFFDocumentHandler {
        public void startDocument(String locator) {
        }
        
        public void commentLine(String comment) {
            if (comment.charAt(0) == '#') {
              /*  try {
                    Matcher regionMatcher = REGION_PATTERN.matcher(comment.substring(1));
                    if (regionMatcher.matches()) {
                        String seqName = regionMatcher.group(1);
                        int length = Integer.parseInt(regionMatcher.group(3));
                        registerSeq(seqName);
                        if (!sequenceLengths.containsKey(seqName)) {
                            sequenceLengths.put(seqName, new Integer(length));
                        }
                    }
                } catch (Exception ex) {
                    log("Error parsing comment", ex);
                }*/
            }
        }
        
        public void recordLine(GFFRecord record) {
            /*allTypes.add(record.getFeature());
            String seq = record.getSeqName();
            registerSeq(seq);
            List seqGFF = (List) gffSets.get(seq);
            if (seqGFF == null) {
                seqGFF = new ArrayList();
                gffSets.put(seq, seqGFF);
            }
            
            seqGFF.add(record);*/
        }
        
        public void endDocument() {    
        }
    }
}
